package com.module.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.module.business.dao.CityDao;
import com.module.business.dao.UsersDao;
import com.module.business.entity.City;
import com.module.business.entity.Users;
import com.module.business.service.ICityService;
import com.module.business.service.IUsersService;
import com.module.business.service.impl.base.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import java.util.List;

//import org.flowable.bpmn.model.BpmnModel;
//import org.flowable.engine.ProcessEngine;
//import org.flowable.engine.ProcessEngineConfiguration;
//import org.flowable.engine.RepositoryService;
//import org.flowable.engine.RuntimeService;
//import org.flowable.engine.TaskService;
//import org.flowable.engine.runtime.Execution;
//import org.flowable.engine.runtime.ProcessInstance;
//import org.flowable.image.ProcessDiagramGenerator;
//import org.flowable.task.api.Task;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengluqiang
 * @since 2019-03-04
 */
@Service
@Slf4j
public class UsersServiceImpl extends BaseServiceImpl<UsersDao, Users> implements IUsersService {
    @Autowired
    private ICityService cityService;
	@Autowired
	private UsersDao usersDao;

    @Override
    public String getMsg(String id) {
    	System.out.println("++++++++++++++++jcache中没有数据++++++++++++");
    	System.out.println("++++++++++++++++到redis中查看++++++++++++");
    	return "str"+id;
    }
    
    @Override
    public String getMsg2(String id) {
    	System.out.println("++++++++++++++++jcache22中没有数据++++++++++++");
    	System.out.println("++++++++++++++++到redis22中查看++++++++++++");
    	return "str2"+id;
    }
    
    @Autowired
    private CityDao cityDao;
    @CacheEvict(cacheNames = "jcache", allEntries=true)
    public String clear() {
        /*Users users = new Users();

        List<Users> usersList = usersDao.selectList(null);
        log.info(usersList.toString()+"++++++++++++++++++");

        List<Users> usersList1 = users.selectAll();
        Page<Users> usersPage = users.selectPage(new Page<>(0, 12), null);

        log.info("usersPage="+usersPage+"----------"+usersPage.getRecords());
        log.info(usersList1.toString()+"---------------");*/
        log.info("clear start =================================");
        City city = new City();
        List<City> cityList = city.selectAll();
        cityDao.selectList(null);
        log.info(cityList.toString()+"---------------");
        return "str1";
    }
    
	@Override
	public String save(String id, String val) {
		
		return "OK!";
	}

	@Override
	public List getAllList() {
		QueryWrapper queryWrapper = new QueryWrapper();
		List<Users> list = usersDao.selectList(queryWrapper);
		Users users = list.size()>0?list.get(0):new Users();
		log.info(users.getUsername());
		return list;
	}

	/*	*//**
	 * 启动任务
	 * @author chenluqiang
	 * @date 2019年3月11日 下午4:06:25 
	 *	@param userId
	 *	@return
	 *//*
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;
	@Override
	public String startProcess(String userId) {
		if(StringUtils.isBlank(userId)) {
			return "error";
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("taskUser", userId);
		map.put("money", "100");
		ProcessInstance instance = runtimeService.startProcessInstanceByKey("Expense",map);
		return "提交成功，流程id为："+instance.getId();
	}
	
	*//**
	 * 通过审核
	 * @author chenluqiang
	 * @date 2019年3月11日 下午4:15:09 
	 *	@param processId
	 *	@return
	 *//*
	@Override
	public String passProcess(String processId) {
		taskService.complete(processId, null);
		return "OK!";
	}
	
	*//**
	 * 任务列表
	 * @author chenluqiang
	 * @date 2019年3月11日 下午4:17:03 
	 *	@param userId
	 *	@return
	 *//*
	@Override
	public List<Task> list(String userId) {
		 List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
	     for (Task task : tasks) {
	         System.out.println(task.toString()+",time:"+task.getCreateTime().toString());
	     }
	     return tasks;
	}
	
	@Autowired
	private ProcessEngine processEngine;
	@Autowired
	private RepositoryService repositoryService;
	@Override
	public void getProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws IOException {
		ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();
		 
        //流程走完的不显示图
        if (pi == null) {
            return;
        }
        Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        //使用流程实例ID，查询正在执行的执行对象表，返回流程实例对象
        String instanceId = task.getProcessInstanceId();
        List<Execution> executions = runtimeService
                .createExecutionQuery()
                .processInstanceId(instanceId)
                .list();
 
        //得到正在执行的Activity的Id
        List<String> activityIds = new ArrayList<>();
        List<String> flows = new ArrayList<>();
        for (Execution exe : executions) {
            List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
            activityIds.addAll(ids);
        }
 
        //获取流程图
        BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
        ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
        ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
        InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows, engconf.getActivityFontName(), engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0, false);
        OutputStream out = null;
        byte[] buf = new byte[1024];
        int legth = 0;
        try {
            out = httpServletResponse.getOutputStream();
            while ((legth = in.read(buf)) != -1) {
                out.write(buf, 0, legth);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
	}*/
	
}
